package com.ruoyi.stock.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.stock.domain.StockUser;
import com.ruoyi.stock.service.IStockConfigService;
import com.ruoyi.stock.service.IStockUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/stock/stockuser")
@Api( tags = "用户管理")
public class StockUserController extends BaseController
{
    @Autowired
    private IStockUserService stockUserService;

    @Autowired
    private IStockConfigService stockConfigService;

    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "用户列表")
    public TableDataInfo list(StockUser stockUser)
    {
        startPage();
        List<StockUser> list = stockUserService.selectStockUserList(stockUser);
        return getDataTable(list);
    }
    // 首页统计;

    @GetMapping(value = "/pcIndexStatic")
    @ApiOperation(value = "首页统计接口")
    public AjaxResult pcIndexStatic()
    {

        AjaxResult ajaxResult=new AjaxResult();
        Map<String,String> resultMap=new HashMap<>();
        List<LocalDate> lastSevenDays = getLastSevenDays();
        Collections.sort(lastSevenDays);
        ///////////////
        List<Map<String, Object>> maplist= stockUserService.selectMemberOrderByDay();
        for( LocalDate dateParam:lastSevenDays){
            resultMap.put(dateParam.toString(),"0");
             for(Map<String, Object> map1:maplist){
                // System.out.println(dateParam+"==========================="+map1.get("dayTime").toString());
                 if(dateParam.toString().equals(map1.get("dayTime").toString())){
                     resultMap.put(dateParam.toString(),map1.get("dayOrderCount").toString());

                 }
             }
        }
        // 排序
        Map<String, Object> sortedMap = new TreeMap<>(Comparator.comparing(String::toString));
        sortedMap.putAll(resultMap);

        ////////////
        Map<String,String> map2= stockUserService.selectMemberOrderCount();
        map2.put("inqueyCount",stockConfigService.selectStockConfigById(2l).getContent());
        ajaxResult.put("dayData",sortedMap);
        ajaxResult.put("totalData",map2);
        return success(ajaxResult);
    }

  // 查询最近七天的日期
    public static List<LocalDate> getLastSevenDays() {
        LocalDate today = LocalDate.now();
        return IntStream.range(0, 7)
                .mapToObj(today::minusDays)
                .collect(Collectors.toList());
    }

    /**
     * 导出用户列表
     */
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockUser stockUser)
    {
        List<StockUser> list = stockUserService.selectStockUserList(stockUser);
        ExcelUtil<StockUser> util = new ExcelUtil<StockUser>(StockUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stockUserService.selectStockUserById(id));
    }

    @GetMapping(value = "/getStockuser")
    public AjaxResult getStockuser()
    {
        long sysUserid= SecurityUtils.getUserId();
        StockUser stockUser=new StockUser();
        stockUser.setSysUserid(sysUserid);
        List<StockUser> list = stockUserService.selectStockUserList(stockUser);
        list.get(0).setPhoneNumber( SecurityUtils.getLoginUser().getUsername());
        return success(list.get(0));
    }

    /**
     * 新增用户
     */
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockUser stockUser)
    {
        return toAjax(stockUserService.insertStockUser(stockUser));
    }

    /**
     * 修改用户
     */
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "更改用户信息")
    @PutMapping
    public AjaxResult edit(@RequestBody StockUser stockUser)
    {
        stockUser.setSysUserid(SecurityUtils.getUserId());
        return toAjax(stockUserService.updateStockUserBySysUserId(stockUser));
    }

    /**
     * 删除用户
     */
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockUserService.deleteStockUserByIds(ids));
    }
}
